﻿using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SHRMS.childForm
{
    public partial class childForm_teaInfo : Form
    {
        private readonly IDbConnection _dbConnection;
        public childForm_teaInfo()
        {
            InitializeComponent();
            _dbConnection = new DbFactory().DefaultDbConnection;
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string deptNo =string.Empty,
                   queryType =string.Empty,
                   SQLstr_select = string.Empty;

            if (comboBox_dept.Text != "")
            {
                string deptName = comboBox_dept.Text;
                string sqlStr = "select departmentNo,deptName from t_department;";
                var mySqlDataReader = _dbConnection.ExecuteReader(sqlStr);
                while (mySqlDataReader.Read())
                {
                    if (mySqlDataReader["deptName"].ToString() == deptName)
                    {
                        deptNo = mySqlDataReader["departmentNo"].ToString();
                    }
                }
                mySqlDataReader.Close();
            }

            if (comboBox_type.Text != "")
            {
                queryType = comboBox_type.Text;
            }

            if (!string.IsNullOrEmpty(deptNo))
            {
                if (!string.IsNullOrEmpty(queryType))
                {
                    if (queryType == "按教师编号")
                    {
                        SQLstr_select = $"select * from t_teacher where departmentNo = '{deptNo}' and teacherNo like '%{textBox_str.Text}%';";
                    }
                    else
                    {
                        SQLstr_select = $"select * from t_teacher where departmentNo = '{deptNo}' and name like '%{textBox_str.Text}%';";
                    }
                }
                else
                {
                    SQLstr_select = $"select * from t_teacher where departmentNo = '{deptNo}';";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(queryType))
                {
                    if (queryType == "按教师编号")
                    {
                        SQLstr_select = $"select * from t_teacher where teacherNo like '%{textBox_str.Text}';";
                    }
                    else
                    {
                        SQLstr_select = $"select * from t_teacher where name like '%{textBox_str.Text}%';";
                    }
                }
                else
                {
                    SQLstr_select = "select * from t_teacher;";
                }
            }
            var result = _dbConnection.Query(SQLstr_select).ToList();
            dataGridView_tea.DataSource = result;
            dataGridView_tea.AutoResizeColumnHeadersHeight();
            dataGridView_tea.AutoResizeColumns();
            dataGridView_tea.AutoResizeRows();
        }

        private void childForm_teaInfo_Load(object sender, EventArgs e)
        {
            string sqlStr = "select deptName from t_department;";
            var mySqlDataReader = _dbConnection.ExecuteReader(sqlStr);
            while (mySqlDataReader.Read())
            {
                comboBox_dept.Items.Add(mySqlDataReader["deptName"]);
            }
            mySqlDataReader.Close();
        }

        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            label_query.Text = comboBox_type.Text + "查询:";
        }
    }
}
